package com.linglan.modules.doctor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linglan.common.api.Condition;
import com.linglan.common.api.Page;
import com.linglan.common.api.Query;
import com.linglan.common.constants.CommonConstants;
import com.linglan.common.utils.PageUtil;
import com.linglan.modules.doctor.dao.BizDoctorDao;
import com.linglan.modules.doctor.dto.result.DoctorResult;
import com.linglan.modules.doctor.entity.BizDoctor;
import com.linglan.modules.doctor.service.BizDoctorService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 业务-医生表(BizDoctor)表服务实现类
 *
 * @author makejava
 * @since 2025-05-04 22:20:23
 */
@Service("bizDoctorService")
public class BizDoctorServiceImpl extends ServiceImpl<BizDoctorDao, BizDoctor> implements BizDoctorService {

    /**
     * 分页查询医生
     * @param query
     *
     * @return
     */
    @Override
    public Page<DoctorResult> findByPage(Query query, Long departmentId) {
        LambdaQueryWrapper<BizDoctor> queryWrapper = Wrappers.lambdaQuery();
        if (departmentId != null) {
            queryWrapper.eq(BizDoctor::getDepartmentId, departmentId);
        }
        queryWrapper.eq(BizDoctor::getIsDeleted, 0);
        queryWrapper.orderByDesc(BizDoctor::getWeight);
        queryWrapper.orderByDesc(BizDoctor::getId);
        IPage<BizDoctor> page = this.page(Condition.getPage(query), queryWrapper);
        IPage<DoctorResult> doctorResultIPage = PageUtil.copyPurePage(page, DoctorResult.class);
        List<BizDoctor> records = page.getRecords();
        if (CollectionUtils.isEmpty(records)) {
            return Page.of(doctorResultIPage);
        }
        List<DoctorResult> resultList = new ArrayList<>();
        for (BizDoctor record : records) {
            DoctorResult doctorResult = getDoctorResult(record);
            resultList.add(doctorResult);
        }
        doctorResultIPage.setRecords(resultList);
        return Page.of(doctorResultIPage);
    }

    /**
     * 根据id查询医生
     *
     * @param id
     * @return
     */
    @Override
    public DoctorResult findById(Long id) {
        LambdaQueryWrapper<BizDoctor> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(BizDoctor::getIsDeleted, 0);
        queryWrapper.eq(BizDoctor::getId, id);
        List<BizDoctor> list = this.list(queryWrapper);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        BizDoctor record = list.get(0);
        DoctorResult doctorResult = getDoctorResult(record);
        return doctorResult;
    }

    private static DoctorResult getDoctorResult(BizDoctor record) {
        DoctorResult doctorResult = new DoctorResult();
        doctorResult.setId(record.getId());
        doctorResult.setHospitalId(record.getHospitalId());
        doctorResult.setDepartmentId(record.getDepartmentId());
        doctorResult.setName(record.getName());
        doctorResult.setAvatar(record.getAvatar());
        doctorResult.setAvatarDetail(record.getAvatarDetail());
        doctorResult.setTitle(record.getTitle());
        String tags = record.getTags();
        if (StringUtils.isNotBlank(tags)) {
            doctorResult.setTags(Arrays.stream(tags.split(CommonConstants.SPLITER)).collect(Collectors.toList()));
        }
        doctorResult.setCzTime(record.getCzTime());
        doctorResult.setScore(BigDecimal.valueOf(record.getScore()));
        doctorResult.setTreatCount(record.getTreatCount());
        doctorResult.setStrength(record.getStrength());
        doctorResult.setWeight(record.getWeight());
        return doctorResult;
    }

    /**
     * 根据ids查询医生
     *
     * @param ids
     * @return
     */
    @Override
    public Map<Long, DoctorResult> findGroupById(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return new HashMap<>();
        }
        LambdaQueryWrapper<BizDoctor> queryWrapper = Wrappers.lambdaQuery();
        if (CollectionUtils.size(ids) == 1) {
            queryWrapper.eq(BizDoctor::getId, ids.get(0));
        } else {
            queryWrapper.in(BizDoctor::getId, ids);
        }
        queryWrapper.eq(BizDoctor::getIsDeleted, 0);
        List<BizDoctor> list = this.list(queryWrapper);
        if (CollectionUtils.isNotEmpty(list)) {
            Map<Long, DoctorResult> map = new HashMap<>();
            for (BizDoctor doctor : list) {
                DoctorResult doctorResult = getDoctorResult(doctor);
                map.put(doctor.getId(), doctorResult);
            }
            return map;
        }
        return new HashMap<>();
    }

    /**
     * 根据关键词搜索医生
     *
     * @param query
     * @param keyword
     * @return
     */
    @Override
    public Page<DoctorResult> searchByPage(Query query, String keyword) {
        LambdaQueryWrapper<BizDoctor> queryWrapper = Wrappers.lambdaQuery();

//        QueryWrapper<BizDoctor> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(keyword)) {
            queryWrapper.and(wrapper -> wrapper.like(BizDoctor::getName, keyword)
                            .or().like(BizDoctor::getTitle, keyword).or().like(BizDoctor::getTags, keyword)
                    .or().like(BizDoctor::getStrength, keyword));
//            queryWrapper.and(wrapper -> wrapper.like("t.name", keyword)
//                    .or().like("t.title", keyword).or().like("t.tags", keyword)
//                    .or().like("t.strength", keyword).or().like("bd.name", keyword))
        }
        queryWrapper.eq(BizDoctor::getIsDeleted, 0);
        queryWrapper.orderByDesc(BizDoctor::getWeight);
        queryWrapper.orderByDesc(BizDoctor::getId);
        IPage<BizDoctor> page = this.page(Condition.getPage(query), queryWrapper);
        IPage<DoctorResult> doctorResultIPage = PageUtil.copyPurePage(page, DoctorResult.class);
        List<BizDoctor> records = page.getRecords();
        if (CollectionUtils.isEmpty(records)) {
            return Page.of(doctorResultIPage);
        }
        List<DoctorResult> resultList = new ArrayList<>();
        for (BizDoctor record : records) {
            resultList.add(getDoctorResult(record));
        }
        doctorResultIPage.setRecords(resultList);
        return Page.of(doctorResultIPage);
    }

    /**
     * 根据ids查询医生
     *
     * @param ids
     * @return
     */
    @Override
    public List<DoctorResult> findByIds(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Collections.emptyList();
        }
        LambdaQueryWrapper<BizDoctor> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(BizDoctor::getIsDeleted, 0);
        queryWrapper.in(BizDoctor::getId, ids);
        List<BizDoctor> list = this.list(queryWrapper);
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        List<DoctorResult> resultList = new ArrayList<>();
        for (BizDoctor bizDoctor : list) {
            resultList.add(getDoctorResult(bizDoctor));
        }
        return resultList;
    }
}

